package com.tangcco.lockwell.controller.admin;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.base.BaseApiController;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.domain.enums.RoleEnum;
import com.tangcco.lockwell.domain.enums.UserStatusEnum;
import com.tangcco.lockwell.service.AuthenticationService;
import com.tangcco.lockwell.service.UserEventLogService;
import com.tangcco.lockwell.service.UserService;
import com.tangcco.lockwell.utility.DateTimeUtil;
import com.tangcco.lockwell.utility.StudentSubjectUtil;
import com.tangcco.lockwell.viewmodel.admin.user.UserLearningProgress;
import com.tangcco.lockwell.viewmodel.admin.user.UserPageRequestVM;
import com.tangcco.lockwell.viewmodel.admin.user.UserResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.tangcco.lockwell.utility.DateToString.dateToString;
@RequestMapping("/api/admin/user")
@RestController("AdminUserController")
public class UserController  extends BaseApiController {
@Resource
UserService userService;
@Resource
UserEventLogService userEventLogService;
    @Resource
    AuthenticationService authenticationService;
    @Resource
    StudentSubjectUtil studentSubjectUtil;

    @Autowired
    public UserController(UserService userService, UserEventLogService userEventLogService) {
        this.userService = userService;
        this.userEventLogService = userEventLogService;

    }
//分页查询, method = RequestMethod.POST)//查询学生
    @RequestMapping("/page/list")
    public RestResponse<PageInfo<User>> pageList(@RequestBody UserPageRequestVM userPageRequestVM) {
        PageHelper.startPage(userPageRequestVM.getPageIndex(), userPageRequestVM.getPageSize());
        Page<User> page = (Page<User>) userService.userPage(userPageRequestVM);
        PageInfo<User> pageInfo = new PageInfo<>(page);
        return RestResponse.ok(pageInfo);
    }
    //用户详情
    @RequestMapping(value = "/select/{id}")
    public RestResponse<UserResponseVM> select(@PathVariable Integer id) {
        User user = userService.getUserById(id);
        UserResponseVM userVm = UserResponseVM.from(user);
        return RestResponse.ok(userVm);
    }
    //修改用户状态
    @RequestMapping(value = "/changeStatus/{id}")
    public RestResponse<Integer> changeStatus(@PathVariable Integer id) {
        User user = userService.getUserById(id);
        UserStatusEnum userStatusEnum = UserStatusEnum.fromCode(user.getStatus());
        Integer newStatus = userStatusEnum == UserStatusEnum.Enable ? UserStatusEnum.Disable.getCode() : UserStatusEnum.Enable.getCode();
        user.setStatus(newStatus);
        user.setModifyTime(dateToString(new Date()));
        userService.updateByIdFilter(user);
        return RestResponse.ok(newStatus);
    }
    //删除用户
    @RequestMapping(value = "/delete/{id}")
    public RestResponse delete(@PathVariable Integer id) {
        //逻辑删除
        userService.deleteByIdStatus(id);
        return RestResponse.ok();
    }

    //编辑用户信息(增加/修改)
    @RequestMapping(value = "/edit")
    public RestResponse<User> edit(@RequestBody @Valid User model) {

        if (model.getUserId() == null) {

//            model.setDeleted(0);
//            model.setIntegral(1000);
//            model.setCreateTime(dateToString(new Date()));
//            StudentSubjectUtil studentSubjectUtil=new StudentSubjectUtil();
//            model.setOptSubject(studentSubjectUtil.getDefaultSubjectId());
//            userService.insert(model);

            User user = modelMapper.map(model, User.class);
            String encodePwd = authenticationService.pwdEncode(model.getPassword());
            user.setIntegral(1000);
            user.setUserUuid(UUID.randomUUID().toString());
            user.setPassword(encodePwd);
            user.setRole(RoleEnum.STUDENT.getCode());
            user.setStatus(UserStatusEnum.Enable.getCode());
            user.setLastActiveTime(DateTimeUtil.dateFormat(new Date()));
            user.setCreateTime(DateTimeUtil.dateFormat(new Date()));
            user.setDeleted(0);
            user.setOptSubject(studentSubjectUtil.getDefaultSubjectId());
            userService.insertByFilter(user);
        } else {
            model.setModifyTime(dateToString(new Date()));
            userService.updateByIdFilter(model);
        }
        return RestResponse.ok();
    }
    //修改
//    @RequestMapping(value = "/update")
//    public RestResponse update(@RequestBody @Valid UserUpdateVM model) {
//        User user = userService.selectById(getCurrentUser().getUserId());
//        modelMapper.map(model, user);
//        user.setModifyTime(dateToString(new Date()));
//        userService.updateByIdFilter(user);
//        return RestResponse.ok();
//    }
    //学生总数
    @RequestMapping(value = "/studentCount")
    public RestResponse<Integer> studentCount() {
        Integer studentCount = userService.selectStudentCount();
        return RestResponse.ok(studentCount);
    }


    //管理员总数
    @RequestMapping(value = "/adminCount")
    public RestResponse<Integer> adminCount() {
        Integer adminCount = userService.selectAdminCount();
        return RestResponse.ok(adminCount);
    }
    //用户学习进度管理
    @RequestMapping(value = "/progress/{userId}")
    public RestResponse<List<UserLearningProgress>> progress(@PathVariable Integer userId) {
        User user = userService.getUserById(userId);
        List<UserLearningProgress> userLearningProgresses = userService.selectProgressByUserName(user);
        return RestResponse.ok(userLearningProgresses);
    }

    //查看用户整体学习进度
    @RequestMapping(value = "/allProgress/{userId}")
    public RestResponse<List<UserLearningProgress>> allProgress(@PathVariable Integer userId) {
        User user = userService.getUserById(userId);
        List<UserLearningProgress> userLearningProgresses = userService.selectAllLearningProgress(user);
        return RestResponse.ok(userLearningProgresses);
    }


    @RequestMapping(value = "/getAllUser")
    public RestResponse<PageInfo<User>> getAllUser(@RequestBody UserPageRequestVM userPageRequestVM) {
            PageHelper.startPage(userPageRequestVM.getPageIndex(), userPageRequestVM.getPageSize());
        Page<User> page = (Page<User>) userService.getAllUser(userPageRequestVM);
        PageInfo<User> pageInfo = new PageInfo<>(page);
        return RestResponse.ok(pageInfo);
    }
    @RequestMapping(value = "/getAllUserList")
    public RestResponse<List<User>> getAllUserList(@RequestBody UserPageRequestVM userPageRequestVM) {
        List<User> userList = userService.getAllUser(userPageRequestVM);
        return RestResponse.ok(userList);
    }


}





















